package com.hzj.act;

import java.util.ArrayList;
import java.util.List;

import com.hzj.myh.R;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;










public class Act1  extends Activity{
	

	class My
	{
		public int a=0;
	}
	class ViewHolder
	{
		public Button button1;
		public Button button2;
		public EditText editText1;
		public int position;
		
		
	}
	
	
	ArrayList<My> list=new ArrayList<My>();
	ListView listView1;
	
	
	
	
	BaseAdapter adapter=new BaseAdapter() {
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			
			
			
//			View view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, null);
//			Button button1= (Button)view.findViewById(R.id.button1);
//			Button button2= (Button)view.findViewById(R.id.button2);
//			button1.setTag(position);
//			button2.setTag(position);
//		   final EditText editText1 = (EditText)view.findViewById(R.id.editText1);
//		    
//		    
//			button1.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				list.get((Integer)v.getTag()).a++;
//				editText1.setText(list.get((Integer)v.getTag()).a+"");
//			}
//		});	
//			
//			
//			button2.setOnClickListener(new OnClickListener() {					
//			@Override
//			public void onClick(View v) {						
//				list.get((Integer)v.getTag()).a--;
//				editText1.setText(list.get((Integer)v.getTag()).a+"");					
//			}
//		});	
//			
//			
//			editText1.setText(list.get(position).a+"");			
		
			
			
			final ViewHolder viewHolder;	
			if(convertView==null)
			{
				convertView=LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, null);
				viewHolder=new ViewHolder();
				viewHolder.button1= (Button)convertView.findViewById(R.id.button1);
				viewHolder.button2= (Button)convertView.findViewById(R.id.button2);
				viewHolder.editText1 = (EditText)convertView.findViewById(R.id.editText1);
				viewHolder.button1.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
					
						list.get(viewHolder.position).a++;
						viewHolder.editText1.setText(list.get(viewHolder.position).a+"");
						Log.i("hzj", " button1:"+viewHolder.position);
						
					}
				});		
				viewHolder.button2.setOnClickListener(new OnClickListener() {					
					@Override
					public void onClick(View v) {						
						list.get(viewHolder.position).a--;
						viewHolder.editText1.setText(list.get(viewHolder.position).a+"");						
					}
				});				
				convertView.setTag(viewHolder);				
			}
			else
			{			
				viewHolder=(ViewHolder)convertView.getTag();				
			}
			
			viewHolder.position=position;
			viewHolder.editText1.setText(list.get(position).a+"");
			
			
			
			return convertView;
		}
		
		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}
	};
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.act1);
		listView1=(ListView)this.findViewById(R.id.listView1);
		
		
		for (int i = 0; i <=30; i++) {
			list.add(new My());
		}
		
		listView1.setAdapter(adapter);
		
		
	}

}
